package com.ruoyi.project.system.area.controller;

import java.util.List;

import com.ruoyi.framework.web.domain.Ztree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.area.domain.Area;
import com.ruoyi.project.system.area.service.IAreaService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 区域Controller
 * 
 * @author Leil
 * @date 2020-04-21
 */
@Controller
@RequestMapping("/system/area")
public class AreaController extends BaseController
{
    private String prefix = "system/area";

    @Autowired
    private IAreaService areaService;

    @RequiresPermissions("system:area:view")
    @GetMapping()
    public String area()
    {
        return prefix + "/area";
    }

    /**
     * 查询区域列表
     */
    @RequiresPermissions("system:area:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Area>  list(Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        return list;
    }

    /**
     * 导出区域列表
     */
    @RequiresPermissions("system:area:export")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        return util.exportExcel(list, "area");
    }

    /**
     * 新增区域
     */
    @GetMapping("/add/{areaId}")
    public String add(@PathVariable("areaId") Integer areaId, ModelMap mmap)
    {
        Area area = this.areaService.selectAreaById(areaId);
        if (area == null) {
            area = new Area();
            area.setId(0);
            area.setAreaName("主目录");
        }
        mmap.put("area", area);
        return prefix + "/add";
    }


    /**
     * 新增保存区域
     */
    @RequiresPermissions("system:area:add")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Area area)
    {
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改区域
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Area area = areaService.selectAreaById(id);
        if (area.getPid() == 0) {
            mmap.put("pAreaName", "主目录");
        } else {
            Area pArea = areaService.selectAreaById(area.getPid());
            mmap.put("pAreaName", pArea == null ? "主目录" : pArea.getAreaName());
        }
        mmap.put("area", area);
        return prefix + "/edit";
    }

    /**
     * 修改保存区域
     */
    @RequiresPermissions("system:area:edit")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Area area)
    {
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除区域
     */
    @RequiresPermissions("system:area:remove")
    @Log(title = "区域", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(areaService.deleteAreaByIds(ids));
    }


    /**
     * 区域树状页面
     */
    @GetMapping("/areaTree/{areaId}")
    public String areaTree(@PathVariable("areaId") Integer areaId, ModelMap mmap)
    {
        mmap.put("area", this.areaService.selectAreaById(areaId));
        return prefix + "/tree";
    }

    @GetMapping("/getAreaTree")
    @ResponseBody
    public List<Ztree> getAreaTree() {
        return this.areaService.getAreaTreeData();
    }

}
